package com.ansion.baseui.base.loading;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.ansion.basetool.preconditions.Preconditions;
import com.ansion.utils.lifecycle.LifeCycleHelper;
import com.ansion.utils.thread.SafeRunnable;
import com.ansion.utils.thread.ThreadPool;
import com.ansion.utils.ResourceUtils;

/**
 * created by zhaoyuntao
 * on 2020/7/4
 * description:
 */
public class TLoadingUtil {

    @Nullable
    public static CustomAlertDialog showLoading(Context context, boolean cancelable) {
        return showLoading(context, cancelable, null, null);
    }

    @Nullable
    public static CustomAlertDialog showLoading(Context context, boolean cancelable, @StringRes Integer textRes) {
        return showLoading(context, cancelable, textRes, null);
    }

    @Nullable
    public static CustomAlertDialog showLoading(Context context, boolean cancelable, @NonNull CharSequence text) {
        return _showLoading(context, cancelable, text, null);
    }

    @Nullable
    public static CustomAlertDialog showLoading(Context context, boolean cancelable, @StringRes Integer textRes, @DrawableRes Integer drawable) {
        return _showLoading(context, cancelable, textRes == null ? null : ResourceUtils.getString(textRes), drawable);
    }

    @Nullable
    private static CustomAlertDialog _showLoading(Context context, boolean cancelable, CharSequence text, @DrawableRes Integer drawable) {
        Activity activity;
        if (context instanceof Activity) {
            activity = (Activity) context;
        } else {
            activity = LifeCycleHelper.findActivity(context);
        }
        if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
            return null;
        }

        CustomAlertDialog dialog = createDialog(activity, cancelable, text, drawable);
        if (Preconditions.isUiThread()) {
            _showLoading(activity, dialog);
        } else {
            ThreadPool.runUi(new SafeRunnable(activity) {
                @Override
                protected void runSafely() {
                    _showLoading(activity, dialog);
                }
            });
        }
        return dialog;
    }

    private static CustomAlertDialog createDialog(Activity activity, boolean cancelable, CharSequence text, @DrawableRes Integer drawable) {
        CustomAlertDialog dialog = new CustomAlertDialog(activity);
        dialog.setCancelable(cancelable);
        dialog.setMessage(text);
        if (drawable != null) {
            try {
                dialog.setUpdateIcon(drawable);
            } catch (Throwable ignore) {
            }
        }
        return dialog;
    }

    private static void _showLoading(Activity activity, CustomAlertDialog dialog) {
        try {
            if (!activity.isDestroyed() && !activity.isFinishing()) {
                dialog.show();
            }
        } catch (Throwable ignore) {
        }
    }

    public static void stopLoading(@Nullable Activity activity, @Nullable CustomAlertDialog loadingDialog) {
        if (Preconditions.isUiThread()) {
            _stopLoading(activity, loadingDialog);
        } else {
            ThreadPool.runMain(() -> _stopLoading(activity, loadingDialog));
        }
    }

    public static void _stopLoading(@Nullable Activity activity, @Nullable CustomAlertDialog loadingDialog) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        if (loadingDialog != null) {
            try {
                loadingDialog.dismiss();
            } catch (Throwable ignore) {
            }
        }
    }
}
